package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalAccountEBike;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.jeecg.modules.rental.resp.MyEBikeResp;
import org.jeecg.modules.rental.service.IRentalAccountEBikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 账户车辆
 * @Author: sean
 * @Date:   2024-12-20
 * @Version: V1.0
 */
@Api(tags="账户车辆")
@RestController
@RequestMapping("/rental/account-ebike")
@Slf4j
public class RentalAccountEBikeController extends JeecgController<RentalAccountEBike, IRentalAccountEBikeService> {
	@Autowired
	private IRentalAccountEBikeService rentalAccountEbikeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rentalAccountEbike
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "账户车辆-分页列表查询")
	@ApiOperation(value="账户车辆-分页列表查询", notes="账户车辆-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RentalAccountEBike>> queryPageList(RentalAccountEBike rentalAccountEbike,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RentalAccountEBike> queryWrapper = QueryGenerator.initQueryWrapper(rentalAccountEbike, req.getParameterMap());
		Page<RentalAccountEBike> page = new Page<RentalAccountEBike>(pageNo, pageSize);
		IPage<RentalAccountEBike> pageList = rentalAccountEbikeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rentalAccountEbike
	 * @return
	 */
	@AutoLog(value = "账户车辆-添加")
	@ApiOperation(value="账户车辆-添加", notes="账户车辆-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_account_ebike:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalAccountEBike rentalAccountEbike) {
		rentalAccountEbikeService.save(rentalAccountEbike);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalAccountEbike
	 * @return
	 */
	@AutoLog(value = "账户车辆-编辑")
	@ApiOperation(value="账户车辆-编辑", notes="账户车辆-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_account_ebike:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalAccountEBike rentalAccountEbike) {
		rentalAccountEbikeService.updateById(rentalAccountEbike);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "账户车辆-通过id删除")
	@ApiOperation(value="账户车辆-通过id删除", notes="账户车辆-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_account_ebike:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rentalAccountEbikeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "账户车辆-批量删除")
	@ApiOperation(value="账户车辆-批量删除", notes="账户车辆-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_account_ebike:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rentalAccountEbikeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "账户车辆-通过id查询")
	@ApiOperation(value="账户车辆-通过id查询", notes="账户车辆-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalAccountEBike> queryById(@RequestParam(name="id",required=true) String id) {
		RentalAccountEBike rentalAccountEbike = rentalAccountEbikeService.getById(id);
		if(rentalAccountEbike==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rentalAccountEbike);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rentalAccountEbike
    */
    //@RequiresPermissions("org.jeecg.modules.demo:rental_account_ebike:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalAccountEBike rentalAccountEbike) {
        return super.exportXls(request, rentalAccountEbike, RentalAccountEBike.class, "账户车辆");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("rental_account_ebike:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalAccountEBike.class);
    }

	 @ApiOperation("我的车辆列表")
	 @RequestMapping(value = "/ebike-list", method = RequestMethod.GET)
	 public Result ebikeList() {
		 List<MyEBikeResp> ebikeList = rentalAccountEbikeService.ebikeList();
		 return Result.OK(ebikeList);
	 }

	 @ApiOperation("绑定车辆接口")
	 @RequestMapping(value = "/bind-ebike", method = RequestMethod.GET)
	 public Result bindEBike(@RequestParam(value="equCode") String equCode) {
		 Long carId = rentalAccountEbikeService.bindEBike(equCode);
		 return Result.OK("rentalpayaccount.successfully.bound.vehicle", carId.toString());
	 }

	 @ApiOperation("解绑车辆接口")
	 @RequestMapping(value = "/unbind-ebike", method = RequestMethod.GET)
	 public Result unBindEBike(@RequestParam(name="ids",required=true) String ids) {
		 rentalAccountEbikeService.unBindEBike(ids);
		 return Result.OK("rentalpayaccount.successfully.unbound.vehicle");
	 }

}
